package com.penghaonan.appframework.widget;

import static android.graphics.Canvas.ALL_SAVE_FLAG;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.penghaonan.appframework.utils.UIUtils;

import xyz.penghaonan.framework.R;

public class RoundedFrameLayout extends FrameLayout {

    private Paint paint, viewPaint, boarderPaint;
    private Path path, boarderPath;
    private RectF rect = new RectF();
    private RectF boarderRect = new RectF();
    private float ltRadius, rtRadius, rbRadius, lbRadius;
    private float[] radiusArray;
    private int boarderColor;
    private int boarderWidth;

    public RoundedFrameLayout(@NonNull Context context) {
        this(context, null);
    }

    public RoundedFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundedFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundedFrameLayout);
            int radius = typedArray.getDimensionPixelSize(R.styleable.RoundedFrameLayout_radius, 0);
            ltRadius = typedArray.getDimensionPixelSize(R.styleable.RoundedFrameLayout_radiusLeftTop, radius);
            rtRadius = typedArray.getDimensionPixelSize(R.styleable.RoundedFrameLayout_radiusRightTop, radius);
            rbRadius = typedArray.getDimensionPixelSize(R.styleable.RoundedFrameLayout_radiusRightBottom, radius);
            lbRadius = typedArray.getDimensionPixelSize(R.styleable.RoundedFrameLayout_radiusLeftBottom, radius);
            boarderWidth = typedArray.getDimensionPixelSize(R.styleable.RoundedFrameLayout_boaderWidth, 0) * 2;
            boarderColor = typedArray.getColor(R.styleable.RoundedFrameLayout_boaderColor, 0);
            typedArray.recycle();
        }

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        paint.setStyle(Paint.Style.FILL);

        boarderPath = new Path();
        boarderPaint = new Paint();
        boarderPaint.setAntiAlias(true);
        boarderPaint.setColor(boarderColor);
        boarderPaint.setStyle(Paint.Style.STROKE);
        boarderPaint.setStrokeWidth(boarderWidth);

        viewPaint = new Paint();
        path = new Path();
        path.setFillType(Path.FillType.INVERSE_WINDING);
        radiusArray = new float[]{ltRadius, ltRadius, rtRadius, rtRadius, rbRadius, rbRadius, lbRadius, lbRadius};

    }

    public void setRadiusDP(float lt, float rt, float rb, float lb) {
        ltRadius = UIUtils.dip2Px(lt);
        rtRadius = UIUtils.dip2Px(rt);
        rbRadius = UIUtils.dip2Px(rb);
        lbRadius = UIUtils.dip2Px(lb);

        radiusArray = new float[]{ltRadius, ltRadius, rtRadius, rtRadius, rbRadius, rbRadius, lbRadius, lbRadius};
        // 底色
        path.reset();
        path.addRoundRect(rect, radiusArray, Path.Direction.CW);
        // 描边
        boarderPath.reset();
        boarderPath.addRoundRect(boarderRect, radiusArray, Path.Direction.CW);

        postInvalidate();
    }

    public void setBoarderColor(int boarderColor) {
        boarderPaint.setColor(boarderColor);
        postInvalidate();
    }

    public void setBoarderWidth(int boarderWidth) {
        this.boarderWidth = boarderWidth * 2;
        resetBoarderPath();
        postInvalidate();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        // 底色
        rect.right = right - left;
        rect.bottom = bottom - top;

        path.reset();
        path.addRoundRect(rect, radiusArray, Path.Direction.CW);

        resetBoarderPath();
    }

    private void resetBoarderPath() {
        boarderPath.reset();
        boarderPath.addRoundRect(rect, radiusArray, Path.Direction.CW);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        int sc = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), viewPaint, ALL_SAVE_FLAG);
        super.dispatchDraw(canvas);
        canvas.drawPath(boarderPath, boarderPaint);
        canvas.drawPath(path, paint);
        canvas.restoreToCount(sc);
    }
}
